package com.ibm.cloud.envcheck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibm.cloud.envcheck.kit.FileUtils;

public class ServiceHandler {
	
	public static Map<Integer,ICheckService> classesMap = new HashMap<Integer ,ICheckService>();
	
	private static List<String> scanPackages = new ArrayList<String>();
	
	public List<Class>clazz = null;
	
	public ICheckService getInstance(int code){
		return classesMap.get(code);
	}
	
	public void init(String packageName){
		clazz = FileUtils.getAllClassByInterface(ICheckService.class, packageName);
		for(Class cla:clazz){
			boolean isImpleOfChecksService = cla.isAssignableFrom(ICheckService.class);
			ICheckService ics = null;
			if(!isImpleOfChecksService){
				continue;
			}
			try {
				ics = (ICheckService) cla.newInstance();
				
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
			classesMap.put(ics.getCode(), ics);
		}
	}
}
